/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.hamacekj.geometr;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import javax.swing.ImageIcon;
import javax.swing.JPanel;


/**
 * Komponenta jednoho záznamu v postupu konstrukce.
 */
public class ListStep extends JPanel {
    private String[] text;
    private ImageIcon[] images;
    private boolean isSelected;

    private int width = 170;
    private int height = 22;

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(width, height);
    }



    /**
     * Při kreslení budou obrázky prokládány textem tak, že první je řetězec
     * s indexem i pak je image s indexem i.
     * @param text řetězce, které se budou vykreslovat
     * @param images obrázky k vykreslení
     * @param isSelected jestli má být vykreslený zvýrazněně
     */
    public ListStep(String[] text, ImageIcon[] images, boolean isSelected) {
        this.text = text;
        this.images = images;
        this.isSelected = isSelected;
        this.height = this.getFontMetrics(this.getFont()).getHeight();
        if(isSelected){
            this.setBackground(Color.YELLOW);
        }else{
            this.setBackground(Color.WHITE);
        }
    }

    @Override
    public void paint(Graphics g) {
        super.paint(g);
        Graphics2D gr = (Graphics2D)g;
        if(this.isSelected){
            Font f = gr.getFont();
            Font newFont = f.deriveFont(Font.BOLD);
            gr.setFont(newFont);
        }
        FontMetrics metrics = gr.getFontMetrics();
        int currentX = 10;
        int fontHeight = metrics.getAscent() + metrics.getLeading();
        for(int i = 0; i < text.length + images.length; i++){
            if(i % 2 == 1){
                gr.drawString(text[i/2], currentX, fontHeight);
                currentX += metrics.stringWidth(text[i/2]);
            }else{
                ImageIcon img = images[i/2];
                if(img == null){
                    continue;
                }
                AffineTransform transform = new AffineTransform();
                int imgWidth = img.getIconWidth();
                int imgHeight = img.getIconHeight();
                double scale = 1;
                if(img.getIconWidth() > 16){
                    scale = 16 / (double)img.getIconWidth();
                    imgWidth = 16;
                    imgHeight = (int)((double)imgHeight * scale);
                }
                if(img.getIconHeight()*scale > fontHeight){
                    scale = fontHeight / (double)img.getIconHeight();
                    imgHeight = fontHeight;
                    imgWidth = (int)((double)img.getIconWidth() * scale);
                }
                int top = metrics.getAscent() + metrics.getLeading() - imgHeight;
                transform.setTransform(scale, 0, 0, scale, currentX, top);
                gr.drawImage(images[i/2].getImage(), transform, null);
                currentX += imgWidth;
            }
        }
    }


}